package cn.tedu.v3;

import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;

import java.io.*;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Scanner;

/**
 *  多人聊天室 Version 3
 *  Server 服务器：找到CS 多线程类
 *  1.读入放入线程中
 *  2.写出直接写在run方法
  */

public class Server {
    private ServerSocket serverSocket;
    public Server() {
        try {
            serverSocket = new ServerSocket(1234);
        } catch (BindException e) {
            System.out.println();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void start()  {
    Socket socket=null;
String ip=null;
        try {
            while (true) {
                System.out.println("等待客户端的连接");
                socket = serverSocket.accept();
               ip = socket.getInetAddress().getHostAddress();
                System.out.println("一个客户端连接上了" + ip + "连接上了");
                new Thread(new CS(socket)).start();
            }
        } catch (IOException e) {
            System.out.println(ip+"客户端链接失败了");
        } finally {
            if (serverSocket!=null&&serverSocket.isClosed()){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    System.out.println("服务器关闭失败");
                }
                System.out.println("服务器关闭成功");
            }
        }

    }
    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }
    //CS多线程类：控制客户端发来的通讯信息
    private class CS implements Runnable {
        private Socket socket;
        private String ip;
        public CS(Socket s){
            this.socket=s;
            ip=socket.getInetAddress().getHostAddress();
        }

        public void run() {
            PrintWriter pw=null;
            try {
                //1.输出流---直接写在run方法中

                //2.输入流读入功能：存入线程中
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
                pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF8")),true);
            new Thread(()->{
                try {
                    String message;
                    while ((message=br.readLine())!=null){
                        System.out.println(ip+"服务器端说："+message);
                        //1.6增加一句---每次客户端发给服务器端内容
                        System.out.println("服务器端请输入：");
                    }
                }
                catch (IOException e) {
                    System.out.println();
                }
            }).start();
            //1.5增加写作操作  【修改目的：读入写出通顺】
                //1.5.1  控制台卡住---输入内容
                System.out.println("输入文字内容：");
                Scanner input=new Scanner(System.in);
                //1.5.2  一直写出
                while (true) {
                  //  String str = null;
                   // str =br.readLine();
                    //System.out.println(ip+"客户端：" + str);
                    String say=input.nextLine();
                    pw.println("服务器端说："+say);  //写出   一行行文字
                    System.out.println("服务器端输入：");
                }

            } catch (UnsupportedEncodingException e) {
                System.out.println();
            } catch (SocketException e) {
                System.out.println();
            } catch (IOException e) {
                System.out.println();
            } finally {  //一定要关闭：先关流  后关socket
               // if (br != null)
                   // try {
                  //      br.close();
                 //   }catch (IOException e){
                  //      System.out.println();
                  //  }
                //2.关闭CS多线程类中---运行的socket通讯类
                 if (pw!=null)
                     try {
                         pw.close();
                     } catch (Exception e) {
                         e.printStackTrace();
                     }
                if (socket!=null&&!socket.isClosed()){
                    System.out.println(ip+"一个客户端退出服务器连接");
                    try {
                        socket.close();
                    } catch (IOException e) {
                        System.out.println(ip+"一个客户端退出链接异常");
                    }
                }

                }
            }
        }
    }

